#ifndef _LOGIC_H_
#define _LOGIC_H_

#include <fstream>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
#include "Storage.h"
#include "History.h"
#include "SearchSortHandler.h"
#include "FloatingTask.h"
#include "DeadlineTask.h"
#include "TimedTask.h"
#include "ICommand.h"
#include "CreateCommand.h"
#include "DeleteCommand.h"
#include "UpdateCommand.h"
#include "DisplayCommand.h"
#include "SearchCommand.h"
#include "CommonConstant.h"

using namespace std;

class Logic{
public:
	enum COMMAND_TYPE{
		CREATE_TASK,DISPLAY_TASK,UPDATE_TASK,DELETE_TASK,UNDO_TASK,REDO_TASK,TRACK_TASK,SEARCH,EXIT,INVALID
	};

	// constructors and getter/setter functions
	Logic();
	~Logic(void);
	void setFileName(string fileName);
	static Logic* getInstance();

	// Input/Ouput UI HANDLER
	string executeCommand(string userCommand);
	COMMAND_TYPE getCommandType();

	// exit operations
	string exitProgram();
	void memoryRelease();

	// file processing component
	void initializeFileStream(string);
	int loadFromFile(); //-1 means cant open, 0 means new file, 1 means load file, using _file_name directly from private memory
	int writeToFile(); // 1 means successful write to file, 0 means unsuccessful write to file
	int checkFileExists(string); // updated
	
private:
	static Logic* theOnlyLogic;
	string fileName;
	COMMAND_TYPE commandType;
	Storage* storage;
	History history;

	// inner methods(NOT ACCESSIBLE TO USER)
	void setUserCommand(COMMAND_TYPE);
	COMMAND_TYPE determineCommandType(string);
	string extractCommand(string);
	string extractRestOfUserCommand(string);
	string toLowerCase(string);
	bool isEqualIgnoreCaseString(string,string);

	// string variables
	static char buffer[255];
	static const string WELCOME_MESSAGE_LOADED_EXISTING_FILE;
	static const string WELCOME_MESSAGE_NEW_FILE;
	static const string WELCOME_MESSAGE_FILE_ERROR;
};

#endif